package com.example.smartall.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.ChangePasswordBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;
import com.google.gson.Gson;

/**
 * @Author: abbas
 * @Description: 修改密码 好像没啥好说的
 */
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener {
    private ImageView imBack;
    private TextView tvTitle;
    private EditText etOld;
    private EditText etNew;
    private Button btChange;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        init();
        initEvent();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        etOld = (EditText) findViewById(R.id.et_old);
        etNew = (EditText) findViewById(R.id.et_new);
        btChange = (Button) findViewById(R.id.bt_change);

        tvTitle.setText("修改密码");
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(this);
        btChange.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.im_back:
                ChangePasswordActivity.this.finish();
                break;
            case R.id.bt_change:
                String newPassword = etNew.getText().toString();
                ChangePasswordBean changePasswordBean = new ChangePasswordBean();
                changePasswordBean.setOldPassword(etOld.getText().toString());
                changePasswordBean.setNewPassword(newPassword);
                Api.config(ApiConfig.CHANGE_USER_PASSWORD, null)
                        .putRequest(new NetCallBack() {
                                        @Override
                                        public void onSuccess(String res) {
                                            showToastSync("修改成功");
                                            destroyThis();
                                        }

                                        @Override
                                        public void onFailure(Exception e) {
                                            etOld.setText("");
                                            etNew.setText("");
                                            showToastSync("修改失败");
                                        }
                                    }, ChangePasswordActivity.this
                                , new Gson().toJson(changePasswordBean));
                break;
        }
    }
}